package cn.com.self.apis.portal.controller;

import cn.com.self.apis.portal.entity.EvaluateEntity;
import cn.com.self.apis.portal.service.intf.IEvaluateService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;


/**
 * @Author CRL
 * @Date 2021/1/31 16:52
 * @Description 评价表
 */
@Controller
@RequestMapping("/evaluate")
@Api(tags = "评价")
public class EvaluateController {

    @Autowired
    IEvaluateService evaluateService;

    @PostMapping(value = "/create")
    @ApiOperation(value = "创建评价")
    public ResponseEntity create(@RequestBody EvaluateEntity entity) {
        entity.setCreateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        entity.setUpdateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        EvaluateEntity evaluateEntity = this.evaluateService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(evaluateEntity)));
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除评价")
    public ResponseEntity deletes(@PathVariable("id") String id) {
        this.evaluateService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    @PostMapping(value = "/update/{id}")
    @ApiOperation(value = "更新评价")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody EvaluateEntity entity) {
        EvaluateEntity evaluateEntity = this.evaluateService.load(id);
        CopyUtils.copyProperties(entity, evaluateEntity);
        evaluateEntity.setUpdateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        EvaluateEntity info = this.evaluateService.update(evaluateEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "分页获取评价")
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort","access_token");
        ICustomPage<EvaluateEntity> pageData = this.evaluateService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }

    @GetMapping(value = {"/load/{id}"})
    @ApiOperation(value = "根据id获取数据")
    public ResponseEntity loadId(@PathVariable("id") String id, HttpServletRequest request) {
        EvaluateEntity evaluateEntity = this.evaluateService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(evaluateEntity));
    }

}
